#!/usr/bin/python
# -*-  coding: UTF8 -*- 

import config
import sys
sys.path.append(config.PROJECT_HOME) #先要把自己的项目目录加入path
from django.core.management import setup_environ #这是重头戏，全靠它了
from stock import settings #介绍自已人
setup_environ(settings) #安排自己人
#干活去吧
import os
import string
import time
import datetime
import numpy
import re
from stock.gphq import models
from django.db import connection, transaction
from workmanager import * 

#具体要做的任务  
# @transaction.commit_on_success
# @transaction.commit_manually
def do_job(args):  
    global g_mutex
    global records
    global pat_in, pat_out
    # global pattern
    recs = []

    if (len(args)==0):
        return False

    filepath = args[0]
    filename = os.path.basename(filepath).upper()
    filedir = os.path.dirname(filepath) + '/'

    if not os.path.isfile(filepath):
        return False


    scjc = 'SZ' if filedir == config.DZH_SZ_F10_HOME else 'SH'
    scdm = '0' if scjc == 'SZ' else '1'
    gpdm = filename[0:6]
    code = scdm + gpdm

    f = open(filepath)
    lines = f.readlines()
    f.close()
    bz = False
    for line in lines:
        if (line.find(pat_in) <> -1):
            bz = True
            continue

        if (line.find(pat_out) <> -1):
            break

        if (bz):
            # result = pattern.findall(line)
            result = line.split()
            if ((len(result) > 1) and (result[0][0:4].isdigit())):
                # print result
                rec = {}
                rec['CODE'] = code
                rec['BGRQ'] = result[0]
                rec['TOTAL'] = string.atof(result[1]) * 10000 if result[1].find('-') == -1 else 0
                rec['FLOW'] = string.atof(result[2]) * 10000 if result[2].find('-') == -1 else 0
                rec['REAL'] = string.atof(result[2]) * 10000 if result[2].find('-') == -1 else 0
                for r in recs:
                    if (r['BGRQ']==rec['BGRQ']):
                        if  (r['FLOW'] < rec['FLOW']):
                            recs.delete(r)
                        else:
                            rec = None
                        break

                if (rec):
                    recs.append(rec)

    g_mutex.acquire()
    print "正在处理 " + scjc + " 市场 " + code + " 股票......"
    for rec in recs:
        records.append(rec)
    g_mutex.release()


@transaction.commit_on_success
def save_gbbq():
    global records

    for rec in records:
        print "正在处理 " + rec['CODE'] + " 股票 " + rec['BGRQ'] + "日数据......"
        gbbq = models.GBBQ(CODE=rec['CODE'], BGRQ=rec['BGRQ'], TOTAL=rec['TOTAL'], FLOW=rec['FLOW'], REAL=rec['REAL'])
        gbbq.save()

if __name__ == '__main__':  
    global g_mutex
    global records
    global pat_in, pat_out
    global pattern

    pat_in = '二、股本变动'.decode('UTF8').encode('GB2312')
    pat_out = '三、分红扩股'.decode('UTF8').encode('GB2312')
    pattern = re.compile(r'(\d{4}-\d{2}-\d{2})\s+(\d+(\.\d+)?)\s+(\d+(\.\d+)?)\s+(.*)')
    # from singleinstance import * 
    g_mutex = threading.Lock()
    records = []

    start = time.time()  
    cursor = connection.cursor()
    cursor.execute('delete from gphq_gbbq')
    transaction.commit_unless_managed()

    print config.DZH_SH_F10_HOME
    files = os.listdir(config.DZH_SH_F10_HOME)
    sh_files = []
    for f in files:
        if (f[7:11] == '011'):
            sh_files.append(config.DZH_SH_F10_HOME + f)
            # do_job([config.DZH_SH_F10_HOME + f, ])

    work_manager =  WorkManager(sh_files, do_job, 10)#或者work_manager =  WorkManager(10000, 20)  
    work_manager.wait_allcomplete()  

    print config.DZH_SZ_F10_HOME
    files = os.listdir(config.DZH_SZ_F10_HOME)
    sz_files = []
    for f in files:
        if (f[7:11] == '011'):
            sz_files.append(config.DZH_SZ_F10_HOME + f)

    work_manager =  WorkManager(sz_files, do_job, 10)#或者work_manager =  WorkManager(10000, 20)  
    work_manager.wait_allcomplete()  

    save_gbbq()

    end = time.time()  
    print end - start
